From 10ac930fdd2371cb050ab286df72df94393c3c85 Mon Sep 17 00:00:00 2001 From: Lee Jeffery Date: Sun, 17 May 2015 13:03:23 +0100 Subject: [PATCH] Added some checks for empty package and target names. --- src/cargo/util/toml.rs | 20 ++++++++++- tests/test_cargo_compile.rs | 66 +++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 1 deletion(-) diff --git a/src/cargo/util/toml.rs b/src/cargo/util/toml.rs index 35450030e..53e4525b6 100644 --- a/src/cargo/util/toml.rs +++ b/src/cargo/util/toml.rs @@ -365,6 +365,10 @@ impl TomlManifest { human("No `package` or `project` section found.") })); + if project.name.trim().is_empty() { + return Err(human("package name cannot be an empty string.")) + } + let pkgid = try!(project.to_package_id(source_id)); let metadata = pkgid.generate_metadata(); @@ -391,6 +395,10 @@ impl TomlManifest { Some(ref bins) => { let bin = layout.main(); + for target in bins { + try!(validate_binary_name(target)); + } + bins.iter().map(|t| { if bin.is_some() && t.path.is_none() { TomlTarget { @@ -518,7 +526,9 @@ impl TomlManifest { } fn validate_library_name(target: &TomlTarget) -> CargoResult<()> { - if target.name.contains("-") { + if target.name.trim().is_empty() { + Err(human(format!("library target names cannot be empty."))) + } else if target.name.contains("-") { Err(human(format!("library target names cannot contain hyphens: {}", target.name))) } else { @@ -526,6 +536,14 @@ fn validate_library_name(target: &TomlTarget) -> CargoResult<()> { } } +fn validate_binary_name(target: &TomlTarget) -> CargoResult<()> { + if target.name.trim().is_empty() { + Err(human(format!("binary target names cannot be empty."))) + } else { + Ok(()) + } +} + fn process_dependencies(cx: &mut Context, new_deps: Option<&HashMap>, mut f: F) -> CargoResult<()> diff --git a/tests/test_cargo_compile.rs b/tests/test_cargo_compile.rs index 42e563855..29ee66fba 100644 --- a/tests/test_cargo_compile.rs +++ b/tests/test_cargo_compile.rs @@ -116,6 +116,72 @@ Caused by: }); +test!(cargo_compile_with_invalid_package_name { + let p = project("foo") + .file("Cargo.toml", r#" + [package] + name = "" + authors = [] + version = "0.0.0" + "#); + + assert_that(p.cargo_process("build"), + execs() + .with_status(101) + .with_stderr("\ +failed to parse manifest at `[..]` + +Caused by: + package name cannot be an empty string. +")) +}); + +test!(cargo_compile_with_invalid_bin_target_name { + let p = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + authors = [] + version = "0.0.0" + + [[bin]] + name = "" + "#); + + assert_that(p.cargo_process("build"), + execs() + .with_status(101) + .with_stderr("\ +failed to parse manifest at `[..]` + +Caused by: + binary target names cannot be empty. +")) +}); + +test!(cargo_compile_with_invalid_lib_target_name { + let p = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + authors = [] + version = "0.0.0" + + [lib] + name = "" + "#); + + assert_that(p.cargo_process("build"), + execs() + .with_status(101) + .with_stderr("\ +failed to parse manifest at `[..]` + +Caused by: + library target names cannot be empty. +")) +}); + test!(cargo_compile_without_manifest { let tmpdir = TempDir::new("cargo").unwrap(); let p = ProjectBuilder::new("foo", tmpdir.path().to_path_buf()); -- 2.30.2